<?php
/**
 * Created by PhpStorm.
 * User: lhl
 * Date: 2020/11/23
 * Time: 13:36
 * Features: 系统伪装
 */

namespace app\api\model;


use app\common\exception\CustomException;
use think\Db;

class HostGuiseConfigModel extends BaseModel
{
    //端口类型
    const PROTOCOLS = [
        6 => 'tcp',
        17 => 'udp',
        1 => 'icmp'
    ];

    /**
     * 伪装信息
     * @param string $hostKey 主机绑定码
     */
    public function disguise($hostKey){
        $host = Db::name('host')->where('hostkey',$hostKey)->find();
        if(!$host){
            throw new CustomException('主机不存在',422);
        }

        $config = $this->where('host_id',$host['id'])->findOrEmpty()->toArray();
        //开始伪装
        if ($config && $config['guise_status'] ) {
            $ports = Db::name('host_guise_port')->field('port,protocols')->where('host_id',$host['id'])->select();
            $guisePort = [];
            foreach ($ports as $port){
                $guisePort[] = [
                    //类型
                    'Proto' => isset(self::PROTOCOLS[$port['protocols']]) ? self::PROTOCOLS[$port['protocols']] : 'unknown',
                    //端口
                    'Port' => $port['port']
                ];
            }

        } else {
            //未开始伪装
            $guisePort = [];
        }

        //修改回调状态
        $this->setCallBackStatus();

        //返回数据
        return [
            'Ports' => $guisePort
        ];
    }

}